SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
CREATE PROCEDURE [dbo].[spCalcularIndices]
@idIndice int
 AS
SET NOCOUNT ON
declare @NProc varchar(30)
declare @Comando varchar(200)
declare @UltimoValor varchar(50)
declare @TipoDato int
declare @cr int

Select @NProc = StoreProcedure from tb_TiposDeAcumuladores where idTipoIndice = @idIndice

declare param cursor forward_only for Select UltimoValor, TipoParametro from tb_ParametrosDeProcedimientos where idIndice = @idIndice for read only
open param

Select @Comando = @NProc+' '

fetch next from param into @UltimoValor, @TipoDato

while @@fetch_status = 0
begin	Select @cr = 1
	if (@TipoDato = 3) or (@TipoDato = 2)
		Select @Comando = @Comando +'"'+@UltimoValor +'"'+ ','
	else
		Select @Comando = @Comando + @UltimoValor + ','
	fetch next from param into @UltimoValor, @TipoDato
end

if @cr = 1
	Select @Comando = left(@Comando,len(@Comando)-1)

print @Comando
exec(@Comando)

close param
deallocate param
GO
